package mina;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
/**
 * 
 * mina 三层结构
 * 1.I/O Service
 * 2.I/O Filter
 * 3.I/O Handler 
 * 
 * @author yang
 *
 * 2016年1月18日
 *
 */
public class TCPServer{
    private static final int PORT = 9123;

    public static void main( String[] args ) throws IOException{
        IoAcceptor acceptor = new NioSocketAcceptor();
        //设置缓存
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        //
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        //启动日志
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        //设置编码
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "utf-8" ))));
        //设置事件处理
        acceptor.setHandler( new TimeServerHandler());
       
        //绑定端口
        acceptor.bind( new InetSocketAddress(PORT) );
    }
}
